<template>
  <div class="user-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="card-title">API调用记录</span>
          <div class="conditions">
            <span>账号：</span>
            <el-select v-model="conditionState.userId" placeholder="请选择">
              <el-option v-for="user in userList" :key="user.userId" :value="user.userId" :label="user.userAccount">{{user.userAccount}}</el-option>
            </el-select>
            <span>模型：</span>
            <el-select v-model="conditionState.model" placeholder="请选择" style="width: 200px" clearable>
              <el-option-group
                  v-for="group in modelList"
                  :key="group.platform"
                  :label="group.name">
                <el-option
                    v-for="item in group.list"
                    :key="group.platform +'[#]'+item.code"
                    :label="group.name + '：' +item.label"
                    :value="group.platform +'[#]'+item.code"/>
              </el-option-group>
            </el-select>
            <span>日期范围：</span>
            <el-date-picker
                v-model="conditionState.dateRange"
                type="daterange"
                unlink-panels
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :shortcuts="shortcuts"/>
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </div>
        </div>
      </template>
      <el-table :data="recordList" style="width: 100%">
        <el-table-column prop="userAccount" label="账号" width="180" >
          <template #default="scope">
            <span>{{scope.row.userInfo ? scope.row.userInfo.userAccount : ''}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="日期" width="160"/>
        <el-table-column prop="inToken" label="[输入|输出|总计]Tokens" >
          <template #default="scope">
            <span>{{scope.row.inToken}}</span>
            <el-divider direction="vertical" />
            <span>{{scope.row.outToken}}</span>
            <el-divider direction="vertical" />
            <span>{{scope.row.totalToken}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="inToken" label="[输入|输出|总计]费用" >
          <template #default="scope">
            <span>{{scope.row.inPrice}}</span>
            <el-divider direction="vertical" />
            <span>{{scope.row.outPrice}}</span>
            <el-divider direction="vertical" />
            <span>{{scope.row.totalPrice}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="model" label="模型"  width="200">
          <template #default="scope">
            <span v-if="modelObj[scope.row.model]">{{modelObj[scope.row.model]}}</span>
            <span v-else>{{scope.row.model}}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  </div>
</template>

<script setup>
import {ref, nextTick} from 'vue'
import api from '../../api/index.js'
import constants from "~/common/constants.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {ArrowDown} from "@element-plus/icons-vue";
import {md5} from 'js-md5'
import Decimal from "decimal.js";

const conditionState = ref({
  userId: '',
  startDate: '',
  endDate: '',
  model: '',
  dateRange: ['', '']
})
const shortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '最近一月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '最近三个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]

const userList = ref([])
const recordList = ref([])
const modelList = ref([])
const modelObj = ref({})

const getAllUserList = async () => {
  userList.value = []
  const res = await api.getAllUser()
  if (res.data) {
    userList.value = res.data
  }
}

const getAllModelList = () => {
  modelList.value = []
  api.getModelList().then((res) => {
    if (res.data) {
      res.data.forEach((item) => {
        const k = item.platform
        const platform = item.name
        item.list.forEach((model) => {
          modelObj.value[`${k}[#]${model.code}`] = `${platform}：${model.label}`
        })
      })
      modelList.value = res.data
    }
  })
}

const getRecordList = () => {
  if (!conditionState.value.userId) {
    ElMessage.error('请选择要查询的用户')
    return
  }
  recordList.value = []
  console.log(conditionState.value.dateRange)
  const params = {
    startDate: '',
    endDate: '',
    userId: conditionState.value.userId,
    model: conditionState.value.model
  }
  if (conditionState.value.dateRange) {
    if (conditionState.value.dateRange[0]) {
      params.startDate = conditionState.value.dateRange[0]
    }
    if (conditionState.value.dateRange[1]) {
      params.endDate = conditionState.value.dateRange[1]
    }
  }
  api.getRecordList(params).then((res) => {
    if (res.data) {
      res.data.forEach((item) => {
        item.totalPrice = new Decimal(item.inPrice).plus(item.outPrice)
      })
      recordList.value = res.data
    }
  })
}
const handleSearch = () => {
  conditionState.value.pageIndex = 1
  getRecordList()
}
getAllUserList().then(() => {
  conditionState.value.userId = userList.value[0].userId
  getRecordList()
})
getAllModelList()
</script>

<style scoped lang="scss">
.pagination-box{
  position: relative;
  padding: 0.5rem 0;
  display: flex;
  justify-content: flex-end;
}
</style>
